package com.common.util;

import org.dromara.email.api.MailClient;
import org.dromara.email.comm.config.MailSmtpConfig;
import org.dromara.email.core.factory.MailFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author P-JWei
 * @date 2023/7/5 9:20
 * @title 邮箱
 * @description
 */
@Component
public class EmailUtil {

    @Value("${mail.password:zuuwrrrvrwaeciaa}")
    private String password;
    @Value("${mail.username:3264150248}")
    private String username;
    @Value("${mail.isAuth:true}")
    private String isAuth;
    @Value("${mail.isSSL:true}")
    private String isSSL;
    @Value("${mail.port:465}")
    private String port;
    @Value("${mail.smtpServer:smtp.qq.com}")
    private String smtpServer;
    @Value("${mail.fromAddress:1297829488@qq.com}")
    private String fromAddress;

    /**
     * 邮箱注册
     *
     * @return Result
     */
    public MailClient getMailClient() {
        MailSmtpConfig config = MailSmtpConfig.builder()
                .password(password)
                .username(username)
                .isAuth(isAuth)
                .isSSL(isSSL)
                .port(port)
                .smtpServer(smtpServer)
                .fromAddress(fromAddress).build();
        MailFactory.put("xyd", config);
        return MailFactory.createMailClient("xyd");
    }

    /**
     *
     * @param mailAddress 接受邮件 地址
     * @param msgHead 消息头部(标题)
     * @param msgContent 消息内容(即展示的消息内容)
     * @return
     */
    public Boolean sendEmail(String mailAddress,String msgHead,String msgContent){
        MailClient mailClient = getMailClient();
        mailClient.sendMail(mailAddress,msgHead,msgContent);
        return true;
    }

    /**
     *
     * @param mailAddress 接受邮件地址 集合
     * @param msgHead 消息头部(标题)
     * @param msgContent 消息内容(即展示的消息内容)
     * @return
     */
    public Boolean sendEmailBatch(List<String> mailAddress, String msgHead, String msgContent){
        MailClient mailClient = getMailClient();
        mailClient.sendMail(mailAddress,msgHead,msgContent);
        return true;
    }

    public static void main(String[] args) {
        MailSmtpConfig config = MailSmtpConfig.builder()
                .password("wfpackqpwqlnbjee")
                .username("210161062")
                .isAuth("true")
                .isSSL("true")
                .port("465")
                .smtpServer("smtp.qq.com")
                .fromAddress("210161062@qq.com").build();
        MailFactory.put("xyd", config);
        MailClient mailClient = MailFactory.createMailClient("xyd");
        mailClient.sendMail("861035695@qq.com","测试邮件","宝贝这是我赵云龙的测试邮件哦 ，谢谢侬，收到后大声的说：收到");
    }

}
